﻿using Dapper;
using PersonalWebsite.Core.CQRS;
using PersonalWebsite.Core.Result;

namespace PersonalWebsite.Application.Queries.Website;

public record GetAbilityVO
{
    public int Id { get; init; }
    public string Name { get; init; }
    public int Percent { get; init; }
}

public record GetAbilityQuery : IQuery<ApiResult<List<GetAbilityVO>>, List<GetAbilityVO>>
{

}

internal class GetAbilityQueryHandler : QueryHandler<GetAbilityQuery, ApiResult<List<GetAbilityVO>>, List<GetAbilityVO>>
{
    private readonly IDBConnectionFactory dBConnectionFactory;

    public GetAbilityQueryHandler(IDBConnectionFactory dBConnectionFactory) : base(dBConnectionFactory)
    {
        this.dBConnectionFactory = dBConnectionFactory;
    }

    public async override Task<ApiResult<List<GetAbilityVO>>> Handle(GetAbilityQuery request, CancellationToken cancellationToken)
    {
        using var dbConnection= dBConnectionFactory.GetDbConnection();

        string sql = "SELECT * FROM WS_Ability";
        var result = await dbConnection.QueryAsync<GetAbilityVO>(sql, cancellationToken);

        return await Task.FromResult(ApiResult<List<GetAbilityVO>>.Success(result?.ToList()));
    }
}